package cn.edu.cug.cs.gtl.plugins.h3dfile.layer;

import cn.edu.cug.cs.gtl.api.wrapper.TableWrapper;
import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.AbstractAction;
import cn.edu.cug.cs.gtl.cga.voxelization.VoxelModeller;
import cn.edu.cug.cs.gtl.io.FileDataSplitter;
import cn.edu.cug.cs.gtl.protos.*;
import com.google.protobuf.Any;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class VoxelizationToFileAction extends AbstractAction {
    @Override
    public void initialize(AbstractApplication theApp) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("h3dfile_resource", Locale.getDefault());

        name=resourceBundle.getString("popupmenu.model3d.layer.voxelization");
        menuItem.setText(name);


        this.theApp = theApp;
        menuItem.addActionListener(this);
        JPopupMenu popupMenu = theApp.asModellerApp().getModel3dUIApp()
                .getProjectPanel()
                .getTreeComponent()
                .getLayerNodePopupMenu();
        popupMenu.addSeparator();
        popupMenu.add(menuItem);
        popupMenu.addSeparator();
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e the event to be processed
     */
    @Override
    public void actionPerformed(ActionEvent e) {
//        final JFileChooser ivFileChooser = new JFileChooser(
//                new File(System.getProperty("user.dir")) );
//        ivFileChooser.setFileFilter(new FileFilter() {
//            @Override
//            public boolean accept(File f) {
//                if ( f == null )
//                    return false;
//                if ( f.isDirectory() )
//                    return true;
//                return f.getName().toLowerCase().endsWith(".vox");
//            }
//
//            @Override
//            public String getDescription() {
//                return "vox File";
//            }
//        });
//
//        ivFileChooser.removeChoosableFileFilter(ivFileChooser.getAcceptAllFileFilter());
//
//        if ( ivFileChooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION )
//            return;
//
//        String fileName = ivFileChooser.getSelectedFile().getAbsolutePath();
        try {
            System.err.println("-------------------------------------------------------------------------------");
            System.err.println("Voxelization Starting...");
            System.err.println("-------------------------------------------------------------------------------");

            Layer layer = theApp.asModellerApp().getModel3dUIApp().getCurrentLayer();
            int xSize = 5, ySize = 5, zSize = 5;
            if(layer != null) {
                ArrayList<VoxelModel> voxelModels = VoxelModeller.voxelize(layer, xSize, ySize, zSize);
                int voxelModelCount = 0;
                for (VoxelModel model : voxelModels) {
                    ++voxelModelCount;
                    if (model == null) {
                        System.err.println("VoxelModel " + voxelModelCount + " is NULL");
                        System.err.println("-------------------------------------------------------------------------------");
                    }
                    else{
                        List<Double> d = model.getVoxelValueList();
                        System.err.println("VoxelModel " + voxelModelCount + " Value: ");
                        System.err.println("-------------------------------------------------------------------------------");
                        for(Double i:d){
                            System.err.println(i.toString());
                        }
                    }
                }
                System.err.println("-------------------------------------------------------------------------------");
                System.err.println("ALL " + voxelModelCount + " VoxelModels");
                System.err.println("Voxelizetion END");
            }
        }
        catch (Exception exception){
            exception.printStackTrace();
        }
    }
}
